//
//  DemoJson.swift
//  swiftDemo
//
//  Created by JIENING ZHANG on 2020/1/13.
//  Copyright © 2020 lovivid. All rights reserved.
//

import UIKit
//import SwiftyJSON

class DemoJson: NSObject {
    
    class func demoLogic() {
    
        let monthArr = ["January","February","March","April","May","June","July","August","September","October","November","December"];

        let monthDic = [
                        "January" :     31,
                        "February" :    28,
                        "March" :       31,
                        "April" :       30,
                        "May" :         31,
                        "June" :        30,
                        "July" :        31,
                        "August" :      31,
                        "September" :   30,
                        "October" :     31,
                        "November" :    30,
                        "December" :    31,
                        ]
        
        let testDic = [
                        "arr" : monthArr,
                        "dic" : monthDic,
                        "str" : "\"zhang\""
            ] as [String : Any]
        
        
        // 对象 --> Data --> String
        if JSONSerialization.isValidJSONObject(testDic) {
            if let jsonData = try? JSONSerialization.data(withJSONObject: testDic, options: .prettyPrinted) {
                let jsonStr = String(data: jsonData, encoding: .utf8)
                if jsonStr != nil {
                    print ("对象 --> Data --> String : \(testDic) --> \(jsonStr!)")
                }
            }
        }
        
        // 使用 GGStringJsonExtension 中 Dictionay 的 ggJsonStr 方法
        let testDicJson = testDic.ggJsonStr()
        if testDicJson != nil {
            print ("GGStringJsonExtension ggJsonStr : \(testDic) --> \(testDicJson!)")
        }
        
        // String --> Data --> 对象
        let jsonStr = "[\"January\", \"March\", \"May\", \"July\", \"August\", \"October\", \"December\", {\"less31\":{\"February\":28, \"April\":30, \"June\":30, \"September\":30, \"November\":30}}]"
        let strData = jsonStr.data(using: .utf8)
        if strData != nil {
            if let jsonObj = try? JSONSerialization.jsonObject(with: strData!, options: .allowFragments) {
                
                if jsonObj is Array<Any> {
                    print ("String --> Data --> Array对象 : \(jsonStr)  ---> \(jsonObj)")
                } else {
                    print ("String --> Data --> Dic对象 : \(jsonStr)  ---> \(jsonObj)")
                }
            }
        }
        
        
        // 使用 GGStringJsonExtension 中 Dictionay 的 ggJsonStr 方法
        let jsonObj = jsonStr.ggJsonObj()
        if jsonObj != nil {
            print ("GGStringJsonExtension ggJsonObj : \(jsonStr) --> \(jsonObj!)")
        }
        
/*
输出：

对象 --> Data --> String : ["arr": ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"], "dic": ["October": 31, "November": 30, "January": 31, "February": 28, "March": 31, "May": 31, "July": 31, "August": 31, "December": 31, "April": 30, "June": 30, "September": 30], "str": "\"zhang\""] --> {
  "arr" : [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
  ],
  "dic" : {
    "October" : 31,
    "November" : 30,
    "January" : 31,
    "February" : 28,
    "March" : 31,
    "May" : 31,
    "July" : 31,
    "August" : 31,
    "December" : 31,
    "April" : 30,
    "June" : 30,
    "September" : 30
  },
  "str" : "\"zhang\""
}
GGStringJsonExtension ggJsonStr : ["arr": ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"], "dic": ["October": 31, "November": 30, "January": 31, "February": 28, "March": 31, "May": 31, "July": 31, "August": 31, "December": 31, "April": 30, "June": 30, "September": 30], "str": "\"zhang\""] --> {
  "arr" : [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
  ],
  "dic" : {
    "October" : 31,
    "November" : 30,
    "January" : 31,
    "February" : 28,
    "March" : 31,
    "May" : 31,
    "July" : 31,
    "August" : 31,
    "December" : 31,
    "April" : 30,
    "June" : 30,
    "September" : 30
  },
  "str" : "\"zhang\""
}
String --> Data --> Array对象 : ["January", "March", "May", "July", "August", "October", "December", {"less31":{"February":28, "April":30, "June":30, "September":30, "November":30}}]  ---> (
    January,
    March,
    May,
    July,
    August,
    October,
    December,
        {
        less31 =         {
            April = 30;
            February = 28;
            June = 30;
            November = 30;
            September = 30;
        };
    }
)
GGStringJsonExtension ggJsonObj : ["January", "March", "May", "July", "August", "October", "December", {"less31":{"February":28, "April":30, "June":30, "September":30, "November":30}}] --> (
    January,
    March,
    May,
    July,
    August,
    October,
    December,
        {
        less31 =         {
            April = 30;
            February = 28;
            June = 30;
            November = 30;
            September = 30;
        };
    }
)
*/
    }
}
